import type {
  PlatformElementConfig,
  StyleConfigMap,
  TiebarFrameworkConfig,
} from '../../interfaces'
import { preprocessElementConfigs } from '../personalization'
import { COMMON_PAGE_ELEMENTS } from './common'

/**
 * 页面元素配置
 */
export const ZHI_HU_QUESTION_PAGE_ELEMENTS: Record<string, PlatformElementConfig> = {
  ...COMMON_PAGE_ELEMENTS,
  // 基础容器
  CONTENT_CONTAINER: {
    id: 'content-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: '.QuestionPage',
    inherit: false,
    styleVar: '--page-content-container',
  },
  QUESTION_CONTAINER: {
    id: 'question-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 20,
    selector: '.QuestionHeader',
    inherit: false,
    styleVar: '--page-question-container',
  },
  DETAIL_WRAPPER: {
    id: 'detail-wrapper',
    category: 'content-container',
    categoryKey: 'category',
    priority: 30,
    selector: '.Question-main .Question-mainColumn',
    inherit: false,
    styleVar: '--page-detail-wrapper',
  },
  DETAIL_CONTAINER: {
    id: 'detail-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 30,
    selector: '.Question-main .Question-mainColumn .Card',
    inherit: false,
    styleVar: '--page-detail-container',
  },
  ANSWERS_CONTAINER: {
    id: 'answers-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 55,
    selector: '.Question-main .Question-mainColumn #QuestionAnswers-answers',
    inherit: false,
    styleVar: '--page-answers-container',
  },
  QUESTION_TAGS: {
    id: 'question-tags',
    category: 'content-container',
    categoryKey: 'category',
    priority: 25,
    selector: '.QuestionHeader .QuestionHeader-tags',
    inherit: false,
    styleVar: '--page-question-tags',
  },
  QUESTION_TITLE: {
    id: 'question-title',
    category: 'title-container',
    categoryKey: 'category',
    priority: 15,
    selector: '.QuestionHeader .QuestionHeader-title',
    inherit: false,
    styleVar: '--page-question-title',
  },
  QUESTION_PROGRESS: {
    id: 'question-progress',
    category: 'content-container',
    categoryKey: 'category',
    priority: 25,
    selector: '.Question-main .Question-mainColumn > .Card',
    inherit: false,
    styleVar: '--page-question-progress',
  },
  ANSWERS_PROFILE: {
    id: 'answers-profile',
    category: 'content-container',
    categoryKey: 'category',
    priority: 35,
    selector: '.Question-main .Question-mainColumn .AnswerItem-authorInfo',
    inherit: false,
    styleVar: '--page-answers-profile',
  },
  ANSWERS_TIME: {
    id: 'answers-time',
    category: 'content-container',
    categoryKey: 'category',
    priority: 40,
    selector: '.Question-main .Question-mainColumn .ContentItem-time span',
    inherit: false,
    styleVar: '--page-answers-time',
  },
  SHORTCUT_WRAPPER: {
    id: 'shortcut-wrapper',
    category: 'content-container',
    categoryKey: 'category',
    priority: 40,
    selector: '.Question-main .ListShortcut .Question-mainColumn .RichContent-inner',
    inherit: false,
    styleVar: '--page-shortcut-wrapper',
  },
  ANSWERS_WRAPPER: {
    id: 'answers-wrapper',
    category: 'content-container',
    categoryKey: 'category',
    priority: 40,
    selector: '.Question-main .Question-mainColumn #QuestionAnswers-answers .RichContent-inner',
    inherit: false,
    styleVar: '--page-answers-wrapper',
  },
  ANSWERS_CONTENT: {
    id: 'answers-content',
    category: 'content-container',
    categoryKey: 'category',
    priority: 40,
    selector: '.Question-main .Question-mainColumn #content',
    inherit: false,
    styleVar: '--page-answers-content',
  },
  ANSWERS_ACTIONS: {
    id: 'answers-actions',
    category: 'content-container',
    categoryKey: 'category',
    priority: 45,
    selector: '.Question-main .Question-mainColumn .ContentItem-actions',
    inherit: false,
    styleVar: '--page-answers-actions',
  },
  CONTENT_LINK: {
    id: 'content-link',
    category: 'content-container',
    categoryKey: 'category',
    priority: 45,
    selector: '.Question-main .Question-mainColumn #content a',
    inherit: false,
    styleVar: '--page-content-link',
  },
}
// 预处理配置
const PROCESSED_ZHI_HU_QUESTION_ELEMENTS = preprocessElementConfigs(ZHI_HU_QUESTION_PAGE_ELEMENTS)

export type ZhiHuQuestionElements = typeof ZHI_HU_QUESTION_PAGE_ELEMENTS
export type ZhiHuQuestionElementKey = keyof typeof ZHI_HU_QUESTION_PAGE_ELEMENTS
export type ZhiHuQuestionElementValue = typeof ZHI_HU_QUESTION_PAGE_ELEMENTS[ZhiHuQuestionElementKey]

/**
 * 框架配置类型
 */
export type ZhiHuQuestionFrameworkConfig = {
  [K in keyof TiebarFrameworkConfig]: Partial<StyleConfigMap>
}

/**
 * 框架配置
 */
export const ZHI_HU_QUESTION_FRAMEWORK_CONFIG: ZhiHuQuestionFrameworkConfig = {
  GLOBAL: {
    shortcuts: [
      PROCESSED_ZHI_HU_QUESTION_ELEMENTS.SHORTCUTS,
    ],
    pageTheme: [
      PROCESSED_ZHI_HU_QUESTION_ELEMENTS.PAGE_THEME,
    ],
    loadingTheme: [
      PROCESSED_ZHI_HU_QUESTION_ELEMENTS.PAGE_LOADING,
    ],
    loadingPhrases: [
      PROCESSED_ZHI_HU_QUESTION_ELEMENTS.PAGE_LOADING,
    ],
    elementHide: [
      PROCESSED_ZHI_HU_QUESTION_ELEMENTS.PAGE_ELEMENTS,
    ],
  },
  WRAPPER: {
  },
  TITLE: {
  },
  META: {
  },
  CONTENT: {
  },
}
